package com.example.flower_shop.service.usr.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.flower_shop.common.PageResult;
import com.example.flower_shop.entity.StoreCart;
import com.example.flower_shop.entity.StoreProduct;
import com.example.flower_shop.exception.exception;
import com.example.flower_shop.mapper.StoreCartMapper;
import com.example.flower_shop.service.adm.StoreProductService;
import com.example.flower_shop.service.usr.StoreCartService;
import com.example.flower_shop.utils.DecimalUtil;
import com.example.flower_shop.vo.cart.CartPageReq;
import com.example.flower_shop.vo.cart.CartReq;
import com.example.flower_shop.vo.cart.CartResp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zbxComputer
 * @description 针对表【sys_store_cart(购物车表)】的数据库操作Service实现
 * @createDate 2024-11-21 12:48:44
 */
@Service
public class StoreCartServiceImpl extends ServiceImpl<StoreCartMapper, StoreCart>
        implements StoreCartService {
    @Resource
    private StoreCartMapper storeCartMapper;
    @Resource
    private StoreProductService productService;

    @Override
    public void batchDelete(Long[] ids) {
        removeByIds(List.of(ids));
    }

    @Override
    public PageResult<CartResp> getCart(CartPageReq req) {
        Page<CartResp> page = new Page<>(req.getPage(), req.getSize());
        req.setUserId(StpUtil.getLoginIdAsLong());
        IPage<CartResp> iPage = storeCartMapper.getCart(page, req);
        return PageResult.of(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords()
                .stream()
                .peek(e -> {
                    if (e.getProductPrice() == null) {
                        e.setProductName("该商品已下架");
                    } else {
                        e.setProductVipPrice(DecimalUtil.vip(e.getProductPrice()));
                    }
                })
                .toList());
    }

    @Override
    public void insertOrUpdateCart(CartReq req) {
        LambdaQueryWrapper<StoreProduct> productWrapper = new LambdaQueryWrapper<>();
        productWrapper.eq(StoreProduct::getId, req.getProductId());
        StoreProduct product = productService.getOne(productWrapper);
        if (product == null) {
            throw new exception("商品不存在");
        }
        LambdaQueryWrapper<StoreCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreCart::getUid, getUserId())
                .eq(StoreCart::getProductId, req.getProductId());
        StoreCart storeCart = storeCartMapper.selectOne(wrapper);
        if (storeCart == null) {
            storeCart = BeanUtil.copyProperties(req, StoreCart.class);
            storeCart.setUid(getUserId());
            int insert = storeCartMapper.insert(storeCart);
            if (insert != 1) {
                throw new exception("添加购物车失败");
            }
        } else {
            if (storeCart.getProductNum() <= 1 && req.getProductNum() < 0) {
                if (!removeById(req.getId())) {
                    throw new exception("删除购物车商品失败");
                }
            } else {
                LambdaUpdateWrapper<StoreCart> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(StoreCart::getId, storeCart.getId())
                        .eq(StoreCart::getProductNum, storeCart.getProductNum());
                updateWrapper.set(StoreCart::getProductNum, storeCart.getProductNum() + req.getProductNum());
                int update = storeCartMapper.update(null, updateWrapper);
                if (update != 1) {
                    throw new exception("更新购物车商品失败");
                }
            }
        }
    }

    @Override
    public void updateCartDirectly(Long id, Integer num) {
        if (num < 1) {
            throw new exception("商品数量不能小于1");
        }
        LambdaUpdateWrapper<StoreCart> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StoreCart::getId, id)
                .eq(StoreCart::getUid, getUserId())
                .set(StoreCart::getProductNum, num);
        int update = storeCartMapper.update(null, updateWrapper);
        if (update != 1) {
            throw new exception("更新购物车商品失败");
        }
    }

    private Long getUserId() {
        return StpUtil.getLoginIdAsLong();
    }
}




